package com.ruoyi.system.controller;

import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.TaskLogTwo;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TaskTwo;
import com.ruoyi.system.service.ITaskTwoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/system/two")
public class TaskTwoController extends BaseController
{
    @Autowired
    private ITaskTwoService taskTwoService;

    /**
     * 查询【任务二】列表
     */
    @PreAuthorize("@ss.hasPermi('system:two:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaskTwo taskTwo)
    {
        startPage();
        List<TaskTwo> list = taskTwoService.selectTaskTwoList(taskTwo);
        return getDataTable(list);
    }

    /**
     * 导出【任务二】列表
     */
    @PreAuthorize("@ss.hasPermi('system:two:export')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskTwo taskTwo)
    {
        List<TaskTwo> list = taskTwoService.selectTaskTwoList(taskTwo);
        ExcelUtil<TaskTwo> util = new ExcelUtil<TaskTwo>(TaskTwo.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【任务二】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:two:query')")
    @Log(title = "二号表详情", businessType = BusinessType.DETAIL, tableIndex = 2)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(taskTwoService.selectTaskTwoById(id));
    }

    /**
     * 新增【任务二】
     */
    @PreAuthorize("@ss.hasPermi('system:two:add')")
    @Log(title = "二号表新增", businessType = BusinessType.INSERT , tableIndex = 2 , isAnalysisJson = true)
    @PostMapping
    public AjaxResult add(@RequestBody TaskTwo taskTwo)
    {
        taskTwoService.insertTaskTwo(taskTwo);

        TaskLogTwo taskLogTwo = new TaskLogTwo();
        taskLogTwo.setTaskId(taskTwo.getId());


        return AjaxResult.success("200",taskLogTwo);
    }

    /**
     * 修改【】
     */
    @PreAuthorize("@ss.hasPermi('system:two:edit')")
    @Log(title = "二号表修改", businessType = BusinessType.UPDATE, tableIndex = 2)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskTwo taskTwo)
    {
        taskTwoService.updateTaskTwo(taskTwo);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id", taskTwo.getId());
        return AjaxResult.success("操作成功",map);
    }

    /**
     * 删除【】
     */
    @PreAuthorize("@ss.hasPermi('system:two:remove')")
    @Log(title = "二号表删除", businessType = BusinessType.DELETE, tableIndex = 2)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        taskTwoService.deleteTaskTwoByIds(ids);
        String id = Arrays.toString(ids).replace("[", "").replace("]", "");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",  id);

        return AjaxResult.success("操作成功",map);
    }




    @PostMapping("/downloadFile")
    public ResponseEntity<Resource> downloadFile(@RequestParam String filePath) {
        try {
            Path file = Paths.get(filePath); // 注意：这里应该添加安全检查，避免路径遍历等安全问题
            Resource resource = new UrlResource(file.toUri());

            if (resource.exists()) {
                return ResponseEntity.ok()
                        .contentType(MediaType.APPLICATION_OCTET_STREAM)
                        .header("Content-Disposition", "attachment; filename=\"" + resource.getFilename() + "\"")
                        .body(resource);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (MalformedURLException e) {
            return ResponseEntity.badRequest().build();
        }
    }










}
